<?php
/**
 * Facade class for the "Hourly Prices" modules
 *
 * @access 	public
 * @author 	Salvatore Borrelli
 * @copyright	ResMania 2011 all rights reserved.
 * @version	1.0
 * @link		http://developer.resmania.com/api
 * @since  	11-2011
 */
class RM_GracePriceBandsConfig extends RM_Model
{
    protected $_name = 'rm_grace_price_bands_config';

    /**
     * Returns unit config value by name.
     *
     * @param int $unitID - unit primary key
     * @param string $name - config name
     * @return mixed - if unit value set, returns unit value. Otherwise default value.
     */
    function fetchValueByUnit($unitID, $name)
    {
        $sql = "
            SELECT
                pc.value as default_value,
                upc.value as unit_value
            FROM
                rm_grace_price_bands_config pc
            LEFT OUTER JOIN
                rm_unit_grace_price_bands_config upc ON upc.name=pc.name AND upc.unit_id=$unitID
            WHERE
                pc.name='$name'
        ";

        $result = $this->_getBySQL($sql)->current();
        if (isset($result->unit_value) && $result->unit_value !== null) {
            return $result->unit_value;
        } elseif (isset($result->default_value)) {
            return $result->default_value;
        }
    }
}